/**
 * 
 */
package fr.gfi.agraf.vue.action.gerer.excel;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import fr.gfi.agraf.exception.AgrafException;
import fr.gfi.agraf.service.IPerimetreService;
import fr.gfi.agraf.utils.PropertiesUtils;
import fr.gfi.agraf.utils.SpringFactory;
import fr.gfi.agraf.vue.action.AbstractApplicationSupportAction;

/**
 * @author Zakaria
 *
 */
public class ExportExcelAction extends AbstractApplicationSupportAction {

	private static final String ERREUR_SAISIE_ID = "erreur.saisie.id";
	private static final String PERIMETRE_SERVICE = "PerimetreService";
	/**
	 * Généré par java
	 */
	private static final long serialVersionUID = -3151280066279151001L;

	/**
	 * Contient l'id et le nom du perimetre
	 */
	private String infoPerimetre;

	/**
	 * Logger
	 */
	private final static Logger LOGGER = Logger.getLogger(ExportExcelAction.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.gfi.agraf.vue.action.AbstractApplicationSupportAction#executeAction()
	 */
	@Override
	public String executeAction() throws Exception {
		final HttpServletResponse response = getServletResponse();
		response.setHeader("Content-Disposition", "attachment; filename=" + getNamePerimetre() + ".xls");
		response.setContentType("application/.xls");
		final OutputStream outputStream = response.getOutputStream();

		// Récupere le service
		final IPerimetreService perimetreService = (IPerimetreService) SpringFactory.getApplicationContext()
				.getBean(PERIMETRE_SERVICE);

		try {
			// Appel à la methode du service
			final HSSFWorkbook workbook = perimetreService.exporterPerimetre(getIdPerimetre());

			workbook.write(outputStream);
			outputStream.close();

		} catch (final AgrafException ae) {
			// Recupére le message d'erreur
			final String messageErreur = PropertiesUtils.getProperty(ae.getKey());
			// Ajoute action error
			addActionError(messageErreur);
			LOGGER.error(messageErreur, ae);
			return ERROR;
		}

		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {
		if (StringUtils.isEmpty(infoPerimetre)) {
			addActionError(ERREUR_SAISIE_ID);
			return;
		}

		final String[] split = infoPerimetre.split("\\|");
		if (split.length < 2) {
			addActionError(ERREUR_SAISIE_ID);
			return;
		} else {
			final Long id = Long.parseLong(split[0]);
			if (id == null) {
				addActionError(ERREUR_SAISIE_ID);
				return;
			}
		}
		super.validate();
	}

	/**
	 * @return the infoPerimetre
	 */
	public String getInfoPerimetre() {
		return infoPerimetre;
	}

	/**
	 * @param infoPerimetre
	 *            the infoPerimetre to set
	 */
	public void setInfoPerimetre(final String infoPerimetre) {
		this.infoPerimetre = infoPerimetre;
	}

	/**
	 * Recupere le nom du perimetre depuis infoPerimetre
	 * 
	 * @return the namePerimetre
	 */
	public String getNamePerimetre() {
		final String[] split = infoPerimetre.split("\\|");
		return split[1].replaceAll(" ", "_");
	}

	/**
	 * Recupere l'id du perimetre depuis inforPerimetre
	 * 
	 * @return the idPerimetre
	 */
	public Long getIdPerimetre() {
		final String[] split = infoPerimetre.split("\\|");
		return Long.parseLong(split[0]);
	}

}
